/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.view.settings;

import java.awt.CardLayout;

import com.genia.toolbox.basics.editor.gui.panel.AbstractSettings;
import com.genia.toolbox.basics.editor.gui.view.settings.panel.EmptySettingsPanel;
import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.form.editor.gui.view.settings.panel.DocumentSettingsPanel;
import com.genia.toolbox.form.editor.gui.view.settings.panel.FormSettingsPanel;
import com.genia.toolbox.form.editor.gui.view.settings.panel.ItemSettingsPanel;
import com.genia.toolbox.form.editor.model.document.impl.DocumentModel;
import com.genia.toolbox.form.editor.model.form.impl.FormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormType;

/**
 * Main element settings panel.
 */
@SuppressWarnings("serial")
public class MainSettingsPanel
    extends AbstractSettings<FormEditorGUI, DocumentModel, FormModel>
{

  /**
   * The displayed settings name.
   */
  private String displayedSettings = null;

  /**
   * The empty panel name.
   */
  private final static String PANEL_EMPTY = "Empty";

  /**
   * The document panel name.
   */
  private final static String PANEL_DEFAULT = "Default";

  /**
   * The form panel name.
   */
  private final static String PANEL_FORM = "Form";

  /**
   * The item panel name.
   */
  private final static String PANEL_ITEM = "SimpleItem";

  /**
   * The empty settings panel.
   */
  private EmptySettingsPanel emptySettingsPanel = null;

  /**
   * The hole settings panel.
   */
  private FormSettingsPanel formSettingsPanel = null;

  /**
   * The document settings panel.
   */
  private DocumentSettingsPanel documentSettingsPanel = null;

  /**
   * The item settings panel.
   */
  private ItemSettingsPanel itemSettingsPanel = null;

  /**
   * The settings layout.
   */
  private CardLayout settingsLayout = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          the application gui.
   */
  public MainSettingsPanel(FormEditorGUI gui)
  {
    super(gui);

    this.settingsLayout = new CardLayout();
    this.setLayout(this.settingsLayout);

    this.emptySettingsPanel = new EmptySettingsPanel();
    this.add(this.emptySettingsPanel, PANEL_EMPTY);
    this.documentSettingsPanel = new DocumentSettingsPanel(this.getGui());
    this.add(this.documentSettingsPanel, PANEL_DEFAULT);
    this.formSettingsPanel = new FormSettingsPanel(this.getGui());
    this.add(this.formSettingsPanel, PANEL_FORM);
    this.itemSettingsPanel = new ItemSettingsPanel(this.getGui());
    this.add(this.itemSettingsPanel, PANEL_ITEM);
    this.itemSettingsPanel.initialise();

    this.settingsLayout.show(this, PANEL_EMPTY);
    this.displayedSettings = null;
  }



  /**
   * Set the displayed document.
   * 
   * @param document
   *          the document.
   */
  @Override
  public void setDocument(DocumentModel document)
  {
    if (this.element != null) {
      this.getGui().notifyElementSettingsChanged(this.getElement());
    }
    if (this.document != null) {
      this.getGui().notifyDocumentSettingsChanged(this.getDocument());
    }
    this.element = null;
    this.document = document;
    if (document != null) {
      this.documentSettingsPanel.setDocument(document);
      this.settingsLayout.show(this, PANEL_DEFAULT);
      this.displayedSettings = PANEL_DEFAULT;
    }
    else {
      this.settingsLayout.show(this, PANEL_EMPTY);
      this.displayedSettings = PANEL_EMPTY;
    }
  }



  /**
   * Set the displayed element.
   * 
   * @param element
   *          the element.
   */
  @Override
  public void setElement(FormModel element)
  {
    if (this.element != null) {
      this.getGui().notifyElementSettingsChanged(this.getElement());
    }
    if (this.document != null) {
      this.getGui().notifyDocumentSettingsChanged(this.getDocument());
    }
    this.element = element;
    this.document = null;
    if (this.element != null) {
      if (FormType.HORIZONTAL.equals(this.element.getType()) || FormType.VERTICAL.equals(this.element.getType()) || FormType.TAB.equals(this.element.getType())
          || FormType.LABELED.equals(this.element.getType())) {
        this.formSettingsPanel.setElement(this.element);
        this.settingsLayout.show(this, PANEL_FORM);
        this.displayedSettings = PANEL_FORM;
      }
      else {
        this.itemSettingsPanel.setElement(this.element);
        this.settingsLayout.show(this, PANEL_ITEM);
        this.displayedSettings = PANEL_ITEM;
      }
    }
    else {
      this.settingsLayout.show(this, PANEL_EMPTY);
      this.displayedSettings = PANEL_EMPTY;
    }
  }



  /**
   * Get the displayed document.
   * 
   * @return the displayed document.
   */
  @Override
  public DocumentModel getDocument()
  {
    return this.documentSettingsPanel.getDocument();
  }



  /**
   * Get the displayed element.
   * 
   * @return the displayed element.
   */
  @Override
  public FormModel getElement()
  {
    FormModel element = null;
    if (this.displayedSettings.equals(PANEL_FORM)) {
      element = this.formSettingsPanel.getElement();
    }
    else if (this.displayedSettings.equals(PANEL_ITEM)) {
      element = this.itemSettingsPanel.getElement();
    }
    return element;
  }

}
